package dtaskboard.client.project.dialog;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.DialogUtil;
import dtaskboard.client.project.ProjectPage;
import dtaskboard.client.service.ProjectService;
import dtaskboard.client.service.ProjectServiceAsync;

public class CreateTaskDialog extends DialogBox {
  private final TextBox taskName;
  private final TextBox taskHours;
  private final String projectName;
  private final Long historyId;

  public CreateTaskDialog(final String projectName, Long historyId) {
    this.taskName = new TextBox();
    this.taskHours = new TextBox();
    this.projectName = projectName;
    this.historyId = historyId;

    Panel panel = new VerticalPanel();

    Label nameLabel = new Label("Nome da tarefa");
    Label hoursLabel = new Label("Horas estimadas");

    panel.add(nameLabel);
    panel.add(this.taskName);
    panel.add(hoursLabel);
    panel.add(this.taskHours);
    panel.add(new CreateTaskSubmitButton());


    super.setAnimationEnabled(true);
    super.setAutoHideEnabled(true);
    super.setHTML("Criação de tarefa");
    super.setGlassEnabled(true);
    super.setModal(true);
    super.setWidget(panel);
    super.setVisible(true);
  }

  @Override
  public void show() {
    super.show();
    this.taskName.setFocus(true);
  }

  class CreateTaskSubmitButton extends Button {
    public CreateTaskSubmitButton() {
      this.setText("Criar tarefa");
      this.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          ProjectServiceAsync projectService = GWT.create(ProjectService.class);

          final CreateTaskDialog dialog = CreateTaskDialog.this;
          String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
          Long estimatedHours = Long.parseLong(dialog.taskHours.getText());
          projectService.createTask(CreateTaskDialog.this.projectName,
              CreateTaskDialog.this.historyId, dialog.taskName.getText(),
              estimatedHours, sessionId, new AsyncCallback<Void>() {

                @Override
                public void onSuccess(Void result) {
                  // DialogUtil.closableDialog("Criação de tarefa",
                  // "Tarefa criada com sucesso.");
                  dialog.hide();
                  new ProjectPage(CreateTaskDialog.this.projectName);
                }

                @Override
                public void onFailure(Throwable caught) {
                  DialogUtil.closableDialog("Criação de tarefa",
                      "Ocorreu um problema ao criar a tarefa.");
                }
              });

        }
      });
    }
  }
}
